﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using VietImage.Models;
using VietImage.Logs;

namespace VietImage.Roles
{
    public abstract class AdministratorRole : RegisteredUserRole
    {
        #region Methods
        public List<Payment> ViewPaymentInformation()
        {
            return Payment.GetList();
        }

        public List<ErrorItem> EditPaymentInformation(Payment p)
        {
            List<ErrorItem> errorList = Payment.ChangeInformation(p, this.UserInfo.ID, 0, this.ID_Language);

            if (errorList == null)
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " changed payment information.");
            else
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " cannot change payment information.");

            return errorList;
        }

        public List<User> ViewBuyer()
        {
            return Payment.GetBuyer();
        }

        public List<Payment> ViewRecentlyBuyer()
        {
            return Payment.GetList(DateTime.Now.AddHours(-24));
        }

        public List<Image> ViewRecentlyDownloadedImageInformation(int count, int from, out int n)
        {
            return Image.GetListDownloadedImage(DateTime.Now.AddHours(-24), count, from, out n);
        }

        public List<Category> ViewCategoryInformation()
        {
            return Category.GetList();
        }

        public List<LogTranslation> ViewTranslationInformation()
        {
            return LogTranslation.GetList();
        }

        public List<User> ViewUserInformation()
        {
            return User.GetList();
        }

        public List<Image> ViewImageInformation(int count, int from, out int n)
        {
            return Image.GetList(count, from, out n);
        }

        public List<Email> ViewEmailInformation()
        {
            return Email.GetList();
        }

        public List<ErrorItem> EditEmailInformation(Email p)
        {
            List<ErrorItem> errorList = Email.ChangeInformation(p, this.UserInfo.ID, this.ID_Language);
            if (errorList == null)
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " changed email information.");
            else
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " cannot change email information.");
            return errorList;
        }

        public List<NoticeEmail> ViewUserNoticeEmailInformation()
        {
            return NoticeEmail.GetListUserEmail();
        }

        public List<NoticeEmail> ViewSubscriberNoticeEmailInformation()
        {
            return NoticeEmail.GetListSubscriberEmail();
        }

        public List<ErrorItem> EditNoticeEmailInformation(NoticeEmail p)
        {
            List<ErrorItem> errorList = NoticeEmail.ChangeInformation(p, this.UserInfo.ID, this.ID_Language);
            if (errorList == null)
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " changed notice email information.");
            else
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " cannot change notice email information.");
            return errorList;
        }

        public List<List<ErrorItem>> SendEmail(List<NoticeEmail> list)
        {
            List<List<ErrorItem>> el = new List<List<ErrorItem>>();
            foreach (NoticeEmail ne in list)
                el.Add(ne.SendEmail(this.UserInfo.ID, this.ID_Language));
            return el;
        }

        public List<NoticeEmail> ViewSentEmail()
        {
            return NoticeEmail.GetListSentEmail();
        }

        public Dictionary<Customer, User> SearchBuyer(String name, String address)
        {
            List<Customer> list = Customer.Search(name, address);

            Dictionary<Customer,User> result = new Dictionary<Customer,User>();

            foreach(Customer c in list)
                result.Add(c,c.GetUser());

            return result;
        }
        #endregion

        #region Reports
        public void DownloadUploadChart(ref int numDownloaded, ref int numUploaded, DateTime date)
        {
            numDownloaded = Image.GetAllNumberOfDownloads(date);
            numUploaded = Image.GetAllNumberOfUploads(date);
        }

        public void DownloadUploadChart(ref int numDownloaded, ref int numUploaded, int month, int year)
        {
            numDownloaded = Image.GetAllNumberOfDownloads(month, year);
            numUploaded = Image.GetAllNumberOfUploads(month, year);
        }

        public Dictionary<User, int> NumberOfDownloadedByPartner(DateTime date)
        {
            Dictionary<User, int> list = new Dictionary<User, int>();

            foreach (User user in User.GetPartner())
                list.Add(user, Image.GetNumberOfDownloadsByUser(date, user.ID));

            return list;
        }

        public Dictionary<User, int> NumberOfDownloadedByPartner(int month, int year)
        {
            Dictionary<User, int> list = new Dictionary<User, int>();

            foreach (User user in User.GetPartner())
                list.Add(user, Image.GetNumberOfDownloadsByUser(month, year, user.ID));

            return list;
        }

        public Dictionary<User, int> NumberOfDownloadedByPartner(DateTime beginDate, DateTime endDate)
        {
            Dictionary<User, int> list = new Dictionary<User, int>();

            foreach (User user in User.GetPartner())
                list.Add(user, Image.GetNumberOfDownloadsByUser(beginDate, endDate, user.ID));

            return list;
        }

        public Dictionary<User, int> NumberOfDownloadedByCustomer(DateTime date)
        {
            Dictionary<User, int> list = new Dictionary<User, int>();

            foreach (User user in User.GetCustomer())
                list.Add(user, Image.GetNumberOfDownloadsByUser(date, user.ID));

            return list;
        }

        public Dictionary<User, int> NumberOfDownloadedByCustomer(int month, int year)
        {
            Dictionary<User, int> list = new Dictionary<User, int>();

            foreach (User user in User.GetCustomer())
                list.Add(user, Image.GetNumberOfDownloadsByUser(month, year, user.ID));

            return list;
        }

        public Dictionary<User, int> NumberOfDownloadedByCustomer(DateTime beginDate, DateTime endDate)
        {
            Dictionary<User, int> list = new Dictionary<User, int>();

            foreach (User user in User.GetCustomer())
                list.Add(user, Image.GetNumberOfDownloadsByUser(beginDate, endDate, user.ID));

            return list;
        }

        //public DataTable DownloadedImageReport(DateTime date)
        //{
        //    VietImageDataClassesDataContext _dbProvider = new VietImageDataClassesDataContext();

        //    IEnumerable<DataRow> temp = (from img in _dbProvider.Images.AsEnumerable()
        //                                 join dimg in _dbProvider.DetailImages.AsEnumerable() on img.ID equals dimg.ID_Image
        //                                 join limg in _dbProvider.LogImages.AsEnumerable() on img.ID equals limg.ID
        //                                 where limg.ActionTime.Date == date.Date && (limg.ID_Action == 7 || limg.ID_Action == 8 || limg.ID_Action == 9)
        //                                 select new
        //                                 {
        //                                     Date = limg.ActionTime,
        //                                     ID_Img = img.ID,
        //                                     NameImg = dimg.Name,
        //                                     AuthorImg = img.Author,
        //                                     CreatedTimeImg = img.CreatedTime,
        //                                     ID_Action = limg.ID_Action,
        //                                     CountOdDownloadeds = img.GetNumberOfDownloads(date)
        //                                 }) as IEnumerable<DataRow>;

        //    return temp.CopyToDataTable();
        //}

        //public DataTable DownloadedImageReport(int month, int year)
        //{
        //    VietImageDataClassesDataContext _dbProvider = new VietImageDataClassesDataContext();

        //    IEnumerable<DataRow> temp = (from img in _dbProvider.Images.AsEnumerable()
        //                                 join dimg in _dbProvider.DetailImages.AsEnumerable() on img.ID equals dimg.ID_Image
        //                                 join limg in _dbProvider.LogImages.AsEnumerable() on img.ID equals limg.ID
        //                                 where limg.ActionTime.Date.Month == month && limg.ActionTime.Date.Year == year && (limg.ID_Action == 7 || limg.ID_Action == 8 || limg.ID_Action == 9)
        //                                 select new
        //                                 {
        //                                     Date = limg.ActionTime,
        //                                     ID_Img = img.ID,
        //                                     NameImg = dimg.Name,
        //                                     AuthorImg = img.Author,
        //                                     CreatedTimeImg = img.CreatedTime,
        //                                     ID_Action = limg.ID_Action,
        //                                     CountOdDownloadeds = img.GetNumberOfDownloads(month, year)
        //                                 }) as IEnumerable<DataRow>;

        //    return temp.CopyToDataTable();
        //}

        //public DataTable DownloadedImageReport(DateTime beginDate, DateTime endDate)
        //{
        //    VietImageDataClassesDataContext _dbProvider = new VietImageDataClassesDataContext();

        //    IEnumerable<DataRow> temp = (from img in _dbProvider.Images.AsEnumerable()
        //                                 join dimg in _dbProvider.DetailImages.AsEnumerable() on img.ID equals dimg.ID_Image
        //                                 join limg in _dbProvider.LogImages.AsEnumerable() on img.ID equals limg.ID
        //                                 where limg.ActionTime.Date >= beginDate.Date && limg.ActionTime.Date <= endDate.Date && (limg.ID_Action == 7 || limg.ID_Action == 8 || limg.ID_Action == 9)
        //                                 select new
        //                                 {
        //                                     Date = limg.ActionTime,
        //                                     ID_Img = img.ID,
        //                                     NameImg = dimg.Name,
        //                                     AuthorImg = img.Author,
        //                                     CreatedTimeImg = img.CreatedTime,
        //                                     ID_Action = limg.ID_Action,
        //                                     CountOdDownloadeds = img.GetNumberOfDownloads(beginDate, endDate)
        //                                 }) as IEnumerable<DataRow>;

        //    return temp.CopyToDataTable();
        //}
        #endregion
    }
}